<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Contacts extends Account_Controller {

    function __construct() {
        parent::__construct();
        //$this->layout->loadLayout("default");
        $this->load->library('rest_caller');
        //$this->load->helper('breadcrumb');
        $this->lang->load('breadcrumb', 'vietnam');
    }

    function index() {

        $url = 'api/contacts_api/getAll';
        $result = $this->rest_caller->post($url);

        $data['all_contacts'] = (isset($result->data) && $result->data) ? $result->data : '';



        $this->layout->view('contacts/list_all_contacts_view', $data);
    }

    function add() {
        if ($this->input->server('REQUEST_METHOD') == "POST") {
            $rules = array(
                array(
                    'field' => 'contact_code',
                    'label' => 'Mã khách hàng',
                    'rules' => 'required|max_length[50]'
                ),
                array(
                    'field' => 'first_name',
                    'label' => 'Họ',
                    'rules' => 'required|max_length[255]'
                ),
                array(
                    'field' => 'last_name',
                    'label' => 'Tên',
                    'rules' => 'required|max_length[255]'
                ),
                
            );
            $this->form_validation->set_rules($rules);
            if ($this->form_validation->run() == TRUE) {
                $contact = array(
                    'contact_code' => $this->input->post('contact_code'),
                    'first_name' => $this->input->post('first_name'),
                    'last_name' => $this->input->post('last_name'),
                    'email' => $this->input->post('email'),
                    'phone' => $this->input->post('phone'),
                    'birth_day' => $this->input->post('birth_day') ? $this->input->post('birth_day') : '',
                    'sex' => $this->input->post('sex') ? $this->input->post('sex') : '',
                    'address' => $this->input->post('address') ? $this->input->post('address') : '',
                    'description' => $this->input->post('description') ? $this->input->post('description') : '',
                    'description' => $this->input->post('description') ? $this->input->post('description') : '',
                    'industry' => $this->input->post('industry') ? $this->input->post('industry') : '',
                    'tax_code' => $this->input->post('tax_code') ? $this->input->post('tax_code') : '',
                );
                $url = 'api/contacts_api/insert';
                
                $result = $this->rest_caller->post($url, $contact);
                if ($result->status == true) {
                    $this->session->set_flashdata('success', "Thêm mới khách hàng thành công.");
                    redirect('contacts');
                } else {
                    $data['form_error_message'] = $result->error;
                }
            }
        }
        $this->layout->view('contacts/add');
    }

}

?>